package org.academik.controller;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.Serializable;
import java.util.ArrayList;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.academik.model.Catalogoservicios;
import org.academik.util.FacesUtil;
import org.academik.util.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;

/**
 *
 * @author Javier
 */
@ManagedBean(name = "catalogoBean")
@ViewScoped
public class CatalogoBean extends AbstractViewScopedBean implements Serializable {

    /** Declaracion de variables*/
    private Catalogoservicios servicio;
    private ArrayList<Catalogoservicios> listaServicios;
    private Catalogoservicios selectedServicio;

    /** Creates a new instance of CatalogoBean */
    public CatalogoBean() {
        this.nuevo();
    }

    @Override
    public void crear() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try {
            tx = (Transaction) session.beginTransaction();
            session.saveOrUpdate(this.servicio);
            session.flush();
            session.clear();
            tx.commit();
            this.nuevo();
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_INFO, "Información:", "El servicio se creo satisfactoriamente"));
        } catch (HibernateException e) {
            tx.rollback();
            FacesUtil.hibernateException(e);
        } finally {
            session.close();
        }
    }

    @Override
    public void eliminar() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        Catalogoservicios s = (Catalogoservicios) session.load(Catalogoservicios.class, this.selectedServicio.getIdCatalogoServicios());
        try {
            tx = (Transaction) session.beginTransaction();
            session.delete(s);
            session.flush();
            session.clear();
            tx.commit();
            this.nuevo();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Información:", "Registro eliminado satisfactoriamente"));
        } catch (HibernateException e) {
            tx.rollback();
            FacesUtil.hibernateException(e);
        } finally {
            session.close();
        }
    }

    @Override
    public void cargarOpciones() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void cargarOpciones(int id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void buscar() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void modificar() {
        this.servicio = this.selectedServicio;
    }

    @Override
    public void cargarResgistros() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void nuevo() {
        this.servicio = new Catalogoservicios();
        this.listaServicios = new ArrayList<Catalogoservicios>();
        this.selectedServicio = null;
        this.populateCatalogo();
    }

    /** Muestra los datos de la BD en la tabla*/
    public void populateCatalogo() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria cCatalogo = session.createCriteria(Catalogoservicios.class);
        cCatalogo.addOrder(Order.asc("nuPrecioDefault"));
        this.listaServicios = (ArrayList<Catalogoservicios>) cCatalogo.list();
        session.close();
    }

    /**
     * Getters & Setters
     */
    public Catalogoservicios getServicio() {
        return servicio;
    }

    public void setServicio(Catalogoservicios servicio) {
        this.servicio = servicio;
    }

    public ArrayList<Catalogoservicios> getListaServicios() {
        return listaServicios;
    }

    public void setListaServicios(ArrayList<Catalogoservicios> listaServicios) {
        this.listaServicios = listaServicios;
    }

    public Catalogoservicios getSelectedServicio() {
        return selectedServicio;
    }

    public void setSelectedServicio(Catalogoservicios selectedServicio) {
        this.selectedServicio = selectedServicio;
    }
}
